package com.htd.apm.Controller;

import com.alibaba.fastjson.JSON;
import com.htd.htdapmapi.alarm.request.AlarmInto;
import com.htd.htdapmapi.alarm.result.AlarmResult;
import com.htd.htdapmapi.common.CommonResult;
import com.htd.htdapmcommon.constants.ErrorCodeConstants;
import com.htd.htdapmservice.service.intf.OneAlertService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/htdAlert")
public class OneAlertController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OneAlertController.class);

    @Autowired
    private OneAlertService oneAlertService;

    /**
     *   统一告警接口
     * @param eventType
     * @param priority
     * @param productType
     * @param alarmsDetail
     * @return
     */
    @RequestMapping(value = "/uniformAlert")
    public String uniformAlert(
            @RequestParam(name = "eventType",required = true) String eventType,
            @RequestParam(name = "priority",required = false) String priority,
            @RequestParam(name = "productType",required = true) String productType,
            @RequestParam(name = "alarmsDetail",required = true) String alarmsDetail,
            @RequestParam(name = "alarmsTitle",required = true) String alarmsTitle){
        try {
            AlarmInto alarmInto=new AlarmInto();
            alarmInto.setEventType(eventType);
            alarmInto.setPriority(priority);
            alarmInto.setProductType(productType);
            alarmInto.setAlarmsDetail(alarmsDetail);
            alarmInto.setAlarmsTitle(alarmsTitle);
            AlarmResult alarmResult=oneAlertService.createAlarm(alarmInto);
            LOGGER.info("调用告警接口返回{}： ",alarmResult);
            return JSON.toJSONString(alarmResult);
        }catch (Exception e){
            LOGGER.error("调用告警服务返回异常",e);
            CommonResult commonResult = new CommonResult();
            commonResult.fail(ErrorCodeConstants.SYSTEM_ERROR_CODE,ErrorCodeConstants.SYSTEM_ERROR_MSG);
            return JSON.toJSONString(commonResult);
        }




    }

}


